package com.bai.ruiji.Controller;

import com.bai.ruiji.Common.BaseContext;
import com.bai.ruiji.Common.R;
import com.bai.ruiji.Service.AddressBookService;
import com.bai.ruiji.pojo.AddressBook;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 27471
 * Time : 2022/10/15 10:54
 */
@RestController//返回JSON数据
@Slf4j//日志
@RequestMapping("/addressBook")
public class AdressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        AddressBook byId = addressBookService.getById(addressBook.getUserId());

        addressBookService.save(addressBook);
        return R.success("添加成功");

    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,addressBook.getUserId());
        addressBookService.remove(queryWrapper);
        addressBookService.save(addressBook);
        return R.success("修改成功");

    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.set(AddressBook::getIsDefault, 0);
        //SQL: update address_book set is_default=0 where user_id=?
        addressBookService.update(queryWrapper);

        addressBook.setIsDefault(1);
        //SQL: update address_book set is_default=1 where id=?
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        AddressBook byId = addressBookService.getById(id);
        if (byId != null) {
            return R.success(byId);
        } else {
            return R.error("没有找到");
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook one = addressBookService.getOne(queryWrapper);
        if (one == null) {
            return R.error("没有找到");
        } else {
            return R.success(one);
        }
    }

    /**
     * 查询指定用户的所有地址
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }
}
